/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.controller;

import com.yanlight.app.request.CompanyAddRequest;
import com.yanlight.app.request.CompanyModifyRequest;
import com.yanlight.app.request.CompanyQueryRequest;
import com.yanlight.app.service.CompanyService;
import com.yanlight.support.http.Response;
import com.yanlight.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author haolw
 * @time 2017/08/18
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @PostMapping("/list")
    public Response list(CompanyQueryRequest request) {
        return ResponseUtil.success(companyService.findList(request));
    }

    @PostMapping("/detail")
    public Response detail(Integer id) {
        return ResponseUtil.success(companyService.findById(id));
    }

    @PostMapping("/add")
    public Response add(CompanyAddRequest request) {
        companyService.add(request);
        return ResponseUtil.success();
    }

    @PostMapping("/modifyStatus")
    public Response modifyStatus(Integer id, Integer status) {
        companyService.modifyStatus(id, status);
        return ResponseUtil.success();
    }

    @PostMapping("/modify")
    public Response modify(CompanyModifyRequest request) {
        companyService.modify(request);
        return ResponseUtil.success();
    }

    @PostMapping("/addPaycode")
    public Response addPaycode(Integer id) {
        companyService.addPaycode(id);
        return ResponseUtil.success();
    }

    @GetMapping("/unifiedQRcode.jpg")
    public void getQR(String paycode, Integer rate, HttpServletResponse response) {

        String code = EncryptUtil.decodeBase64(paycode);
        PayQRcodeUtil.creatQRImage(code, rate, response);
        return;
    }

    @GetMapping("/downloadQR.jpg")
    public void downloadQR(String paycode, String type, Integer rate, String name, HttpServletResponse response) {

        String code = EncryptUtil.decodeBase64(paycode);
        PayQRcodeUtil.createDownloadQRImage(code, rate, name,type, response);
        return;
    }

    @PostMapping("/delete")
    public Response delete(Integer companyId) {
        Integer userId = SessionUtil.getUserId();
        companyService.delete(companyId);
        return ResponseUtil.success();
//        if (userId == 1) {
//            companyService.delete(companyId);
//            return ResponseUtil.success();
//        } else {
//            return ResponseUtil.fail(ErrorType.NO_AUTH.getCode(), ErrorType.NO_AUTH.getEnMsg());
//        }
    }
}
